/****************************************************************************
* VLC-Qt - Qt and libvlc connector library
* Copyright (C) 2015 Tadej Novak <tadej@tano.si>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

#include <QtGui/QMouseEvent>
#include <QtGui/QWheelEvent>

#if QT_VERSION >= 0x050000
    #include <QtWidgets/QProgressBar>
#else
    #include <QtGui/QProgressBar>
#endif

#include "qextVLCError.h"
#include <qextVLCMediaPlayer.h>

#include "qextVLCWidgetSeekProgress.h"

QEXTVLCWidgetSeekProgress::QEXTVLCWidgetSeekProgress(QEXTVLCMediaPlayer *player, QWidget *parent)
    : QEXTVLCWidgetSeek(player, new QProgressBar(), true, parent)
    , m_lock(false)
{
    m_progress->setMaximumHeight(15);
}

QEXTVLCWidgetSeekProgress::QEXTVLCWidgetSeekProgress(QWidget *parent)
    : QEXTVLCWidgetSeek(new QProgressBar(), true, parent)
    , m_lock(false)
{
    m_progress->setMaximumHeight(15);
}

QEXTVLCWidgetSeekProgress::~QEXTVLCWidgetSeekProgress() {}

void QEXTVLCWidgetSeekProgress::mouseMoveEvent(QMouseEvent *event)
{
    event->ignore();

    if (!m_lock)
    {
        return;
    }

    updateEvent(event->pos());
}

void QEXTVLCWidgetSeekProgress::mousePressEvent(QMouseEvent *event)
{
    event->ignore();

    lock();
}

void QEXTVLCWidgetSeekProgress::mouseReleaseEvent(QMouseEvent *event)
{
    event->ignore();

    updateEvent(event->pos());

    unlock();
}

void QEXTVLCWidgetSeekProgress::wheelEvent(QWheelEvent *event)
{
    event->ignore();

    if (!m_vlcMediaPlayer)
    {
        return;
    }

    if (event->delta() > 0)
    {
        m_vlcMediaPlayer->setTime(m_vlcMediaPlayer->time() + m_vlcMediaPlayer->length() * 0.01);
    }
    else
    {
        m_vlcMediaPlayer->setTime(m_vlcMediaPlayer->time() - m_vlcMediaPlayer->length() * 0.01);
    }
}

void QEXTVLCWidgetSeekProgress::setSliderWidget(QWidget *slider, bool updateSlider)
{
    Q_UNUSED(slider)
    Q_UNUSED(updateSlider)

#if QT_VERSION >= 0x050000
    Q_UNIMPLEMENTED();
#endif
    Q_ASSERT(!"QEXTVLCWidgetSeekProgress::setSliderWidget() - Changing the slider widget is not allowed.");
}

void QEXTVLCWidgetSeekProgress::updateEvent(const QPoint &pos)
{
    if (!m_vlcMediaPlayer)
    {
        return;
    }

    if (pos.x() < m_progress->pos().x() || pos.x() > m_progress->pos().x() + m_progress->width())
    {
        return;
    }

    float click = pos.x() - m_progress->pos().x();
    float op = m_progress->maximum() / m_progress->width();
    float newValue = click * op;

    m_vlcMediaPlayer->setTime(newValue);
    m_progress->setValue(newValue);
}

void QEXTVLCWidgetSeekProgress::updateCurrentTime(int time)
{
    if (m_lock)
    {
        return;
    }

    QEXTVLCWidgetSeek::updateCurrentTime(time);
}

void QEXTVLCWidgetSeekProgress::updateFullTime(int time)
{
    if (m_lock)
    {
        return;
    }

    QEXTVLCWidgetSeek::updateFullTime(time);
}

void QEXTVLCWidgetSeekProgress::lock()
{
    m_lock = true;
}

void QEXTVLCWidgetSeekProgress::unlock()
{
    m_lock = false;
}
